/** @file    IWidget.cpp
 *  @author  Alessandro Polo
 *  @version $Id: IWidget.cpp 2878 2010-08-09 01:11:12Z alex $
 *  @brief
 * File containing methods for the wosh::gui::IWidget class.
 * The header for this class can be found in IWidget.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include "IWidget.h"

 #include <core/ObjectAllocator.h>
 #include <core/Bus.h>
 #include <core/Response.h>
 #include <framework/gui/GuiUtilities.h>
 #include <QSignalMapper>
 #include <QtGui/QMessageBox>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER_INTERFACE(wosh::gui::IWidget, "", 1.00, _static_IWidget )

 WOSH_REGISTER_INTERFACE(wosh::gui::IWidgetSerializable, "", 1.00, _static_IWidgetSerializable )

 WOSH_REGISTER_INTERFACE(wosh::gui::IWidgetGeneric, "wosh::gui::IWidget", 1.00, _static_IWidgetGeneric )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT IWidget::init() {
	if ( this->myURI.isKernelEmpty() )
		this->myURI.setKernelLocal();
	if ( this->myURI.getName().empty() )
		this->myURI.setName( Utilities::format("%s#%ld", getClassName(), Utilities::randomLong()) );
	BusGui.setMessageHandler(this);
	WRESULT bus_connected = BusGui.connect( _Bus_Gui, Bus::ACTION_DEFERRED );
	return bus_connected;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void IWidget::postMessageForTarget( Message* message ) {
	message->setSource( this->myURI );

	if ( message->getDestination().isEmpty() )
		message->setDestination( this->targetURI );

	if ( message->getDestinationBus().empty() )
		message->setDestinationBus( _Bus_Core );

	if ( !message->hasSecurityToken() )
		GuiSession::getInstance().signMessage(message);

	BusGui.postMessage(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

IWidgetGeneric::IWidgetGeneric(QWidget *parent ) : QWidget(parent), IWidget() {
	this->signalMapperBtn = new QSignalMapper(this);
	connect(this->signalMapperBtn, SIGNAL(mapped(const QString &)), this, SLOT(executeRemoteMethod(const QString &)));
	this->myURI.setName( Utilities::format("%s#%10lu", getClassName(), Utilities::randomLong()) );
	this->myURI.setKernelLocal();
 }

IWidgetGeneric::~IWidgetGeneric() {
	delete this->signalMapperBtn; this->signalMapperBtn = NULL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void IWidgetGeneric::mapMethodButton( const string& method_name, QObject* sender_button ) {
	connect(sender_button, SIGNAL(clicked()), this->signalMapperBtn, SLOT(map()));
	this->signalMapperBtn->setMapping(sender_button, method_name.c_str() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IWidgetGeneric::promptMethodResponse( const wosh::Message& message, bool filter_mapped, bool filter_failed, bool filter_target ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return false;
	if ( filter_target && !URI::pregMatch(message.getDestination(), this->getWidgetURI()) ) return false;
	const Response* response = message.getContent()->asResponse();
	if ( filter_mapped ) {
		QObject* objMapped = this->signalMapperBtn->mapping( response->getMethod().c_str() );
		if ( objMapped == NULL )
			return false;
	 }
	else if ( filter_failed && WSUCCEEDED(response->getReturnValue()) ) return false;
	GuiUtilities::promptMethodResponse(response, this->windowTitle() );
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
